package com.coocaa.launcher.wallpaper;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.View;
import android.widget.FrameLayout;

/**
 * Created by luwei on 15-7-10.
 */
public class AnimatorUtils {

    public static void translateX(final View view, int fromX, int toX, long during) {
        ValueAnimator animator = ValueAnimator.ofInt(fromX, toX);
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();
        final int init = params.leftMargin;
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();
                params.leftMargin = init + ((Integer) animation.getAnimatedValue());
                view.setLayoutParams(params);
            }
        });
        animator.setDuration(during);
        animator.start();
    }

    public static void translateY(final View view, int fromY, int toY, long during) {
        ValueAnimator animator = ValueAnimator.ofInt(fromY, toY);
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();
        final int init = params.topMargin;
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();
                params.topMargin = init + ((Integer) animation.getAnimatedValue());
                view.setLayoutParams(params);
            }
        });
        animator.setDuration(during);
        animator.start();
    }

    public static void alpha(View view, float fromAlpha, float toAlpha) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", fromAlpha, toAlpha);
        animator.setDuration(300);
        animator.start();
    }
}
